<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>询价单管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" type="text/css" href="../../common/bootstrap/css/bootstrap.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../common/global.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../css/customer_list.css" media="all">
</head>
<style>
    #roleAuthTable + .layui-table-view .layui-table tbody tr:hover {
        background-color: transparent;
    }
    tbody tr .layui-table-cell {
        height: auto !important;
    }
    tbody tr td:last-child .layui-table-cell{
        white-space: normal !important; /* 允许多行显示 */
        word-break: break-all;          /* 长单词或 URL 地址换行 */
    }
    tbody tr td:nth-child(8) .layui-table-cell{
        white-space: normal !important; /* 允许多行显示 */
        word-break: break-all;          /* 长单词或 URL 地址换行 */
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar layui-form-pane">
                <form lay-filter="searchStationForm" id="searchStationForm" class="layui-form model-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">询价来源</label>
                            <div class="layui-input-inline">
                                <input name="bus_from_company_name" id="bus_from_company_name" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">询价单号</label>
                            <div class="layui-input-inline">
                                <input name="bus_inquiry_no" id="bus_inquiry_no" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>


                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">询价人</label>
                            <div class="layui-input-inline">
                                <input name="bus_user" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">询价时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="due_date" placeholder="时间段" class="layui-input" id='due_date'>
                            </div>
                        </div>
                    </div>
                        <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">车牌号</label>
                            <div class="layui-input-inline">
                                <input name="vehicle_license_num" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">车架号</label>
                            <div class="layui-input-inline">
                                <input name="vehicle_frame_no" id="vehicle_frame_no" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">车辆品牌</label>
                            <div class="layui-input-inline">
                                <input name="brand_name" id="brand_name" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">车型</label>
                            <div class="layui-input-inline">
                                <input name="car_model" id="car_model" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>
                        </div>
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">配件商</label>
                            <div class="layui-input-inline">
                                <input name="bus_to_company_name" id="bus_to_company_name" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">修理厂</label>
                            <div class="layui-input-inline">
                                <input name="repair_company" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">报价状态</label>
                            <div class="layui-input-inline mr0">
                                <select id="state" name="state">
                                    <option value="">全部</option>
                                    <option value="1">未报价</option>
                                    <option value="2">已报价</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label w-auto">业务员</label>
                            <div class="layui-input-inline">
                                <input name="add_user_name" class="layui-input" type="text" placeholder="" autocomplete="off"/>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索
                            </button>
                            <button id="btnReset" class="layui-btn layui-btn-warm"><i class="layui-icon">&#xe669;</i>重置
                            </button>
                        </div>
                    </div>
                </form>
            </div>

            <table class="layui-table" id="infoTable" lay-filter="infoTable"></table>
        </div>
    </div>
</div>
<script type="text/html" id="infoTableBar">
    {{# if(d.sale_price_state > 1){ }}
    <!--<a class="layui-btn-a" lay-event="detailQuote">报价详情</a>-->
    {{# } }}
    {{# if(d.state !== 3){ }}
    <a class="layui-btn-a" lay-event="close">关闭</a>
    {{# }else{ }}
    <a class="layui-btn-a" style="background-color: #029789;" lay-event="openInfo">开启</a>
    {{# } }}
    <!--{{# if(d.state == 1){ }}-->
    <!--<a class="layui-btn-a" lay-event="complete">完成</a>-->
    <!--{{# } }}-->
    {{# if(d.opt_role_id == 1){ }}
    <a class="layui-btn-a btn-red" lay-event="del">删除</a>
    {{# } }}
</script>
<!-- 表格状态列 -->
<script type="text/html" id="infoTbaleState">
    <input type="checkbox" lay-filter="ckState" lay-skin="switch" lay-text="正常|关闭"
           value="{{d.id}}" {{d.state==1?'':'checked'}}/>
</script>
<script type="text/html" id="add_user_tpl">
    {{# if(d.opt_role_id == 1){ }}
    <select name="add_user_id" data-id={{d.id}} data-value={{d.add_user_id}} data-text={{d.add_user_name}} lay-filter="select_user" >
<!--        <option value="">请选择</option>-->
        {{# if(window.userList.length > 0){ }}
        {{#  layui.each(window.userList, function(index, item){ }}
        <option value="{{item.user_id}}" {{# if(item.user_id == d.add_user_id){ }} selected{{# }}}>{{item.true_name}}</option>
        {{#  }); }}}
    </select>
    {{# }else{ }}
        {{ d.add_user_name }}
    {{# } }}
</script>
<script type="text/javascript" src="../../js/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="../../js/jquery.cookie.js"></script>
<script type="text/javascript" src="../../common/layui/layui2.js"></script>
<script type="text/javascript" src="../../js/common.js"></script>
<script type="text/javascript" src="../../js/main.js"></script>
<!-- js部分 -->
<script>
    var userList={};
    layui.use(['layer', 'form', 'table','admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var user_info =  JSON.parse(getSessionStorage('uanme'));

        laydate.render({
            elem: '#due_date'
            ,range: true
            ,trigger: 'click'
        });

        layer.load(1);
        send_req('Common/getUserList', {}, function (res) {
            layer.closeAll('loading');
            userList = res;
        });


        form.render();

        //渲染表格
        table.render({
            elem: '#infoTable',
            url: access_baseurl+'Inquiry/getList',
            where: {
                opt_role_id:user_info.role_id,
                opt_user_id:user_info.user_id
            },
            page: true,
            limit:20,
            // cellMinWidth: 100,
            cols: [[
                {field: 'id',hide:true},
                {field: 'bus_inquiry_no', title: '询价单号',width: 220,templet: function (d) {
                        return "<a lay-event='detail' style='border-bottom: 1px solid blue;color: blue;'>"+d.bus_inquiry_no+"</a>";
                    }},
                {field: 'bus_from_company_name', title: '询价来源',width:90},
                {field: 'vehicle_frame_no', title: '车架号',width: 190},
                {field: 'vehicle_license_num', title: '车牌号',width: 110},
                {field: 'brand_name', title: '车辆品牌'},
                {field: 'car_model', title: '车型名称'},
                {field: 'bus_user_name', title: '询价人',width: 90},

                /*{field: 'city_info', title: '地区'},
                {field: 'add_datetime', title: '询价日期',width: 180,
                    templet:function (d) {
                        return d.add_datetime + ' 至 '+ d.deadline_datetime;
                    }},*/
                {field: 'is_expired', title: '是否过期',width: 90,
                    templet:function (d) {
                        if (d.is_expired === 1) {
                            return "<span style='color: #fd1e11'>已过期</span>";
                        }else {
                            return "<span style='color: #11b7f4'>未过期</span>";
                        }
                    }},
                {field: 'sale_price_state', title: '客户报价状态',width: 120,
                    templet:function (d) {
                        if (d.sale_price_state > 1) {
                            return "<span style='color: #11b7f4'>已报价</span>";
                        }else {
                            return "<span style='color: #969595'>未报价</span>";
                        }
                    }
                },
                {title: '配件商报价状态',width: 130,
                    templet:function (d) {
                        return "已报价"+ d.has_quote+"/"+d.sum_quote;
                    }
                },
                // {field: 'add_user_name', title: '业务员',width: 90},
                {align: 'center',toolbar: '#add_user_tpl',title: '业务员',width: 120},
                {align: 'center', toolbar: '#infoTableBar', title: '操作',width: 120}
            ]],
            done: function (res, curr, count) {
                page = res.page;
                res.data.forEach(function (index, item) {//根据已有的值回填下拉框
                    layui.each($("select[name='add_user_id']", ""), function (index, item) {
                        var elem = $(item);
                        var name = elem.attr("data-text");
                        elem.val(elem.data('add_user_id')).parents('div.layui-table-cell').css('overflow', 'visible');
                        elem.next().children().children()[0].defaultValue = name;
                    });
                });
                table.render('select');
            }
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var where = getWhere();
            table.reload('infoTable', {where: where});
            return false;
        });
        $('#btnReset').click(function () {
            var t = $('#searchStationForm [name]').serializeArray();
            var data={};
            $.each(t, function() {
                data[this.name] = '';
            });
            form.val('searchStationForm',data);
            form.render();
            table.reload('infoTable', {where: data});
            return false;
        });
        // 工具条点击事件
        table.on('tool(infoTable)', function (obj) {
            var data = obj.data;
            // console.log(data)
            if (obj.event === 'edit') { //修改
                // location.replace('./add.html?id='+data.id);
                window.parent.go_to_page_general('./html/inquiry/add.html','id='+data.id);
            } else if (obj.event === 'detail') { //修改
                // location.replace('./detail.html?id='+data.id);
                window.parent.go_to_page_general('./html/inquiry/detail.html','id='+data.id);
                // window.open('./detail.html?id='+data.id);
            }else if (obj.event === 'detailQuote') { //修改
                var supplier_id = data.from_quotation_selection_json.add_company_id
                var org_profit_rate = data.from_quotation_selection_json.org_profit_rate==undefined?'':data.from_quotation_selection_json.org_profit_rate
                var matching_profit_rate = data.from_quotation_selection_json.matching_profit_rate==undefined?'':data.from_quotation_selection_json.matching_profit_rate
                var complete_profit_rate = data.from_quotation_selection_json.complete_profit_rate==undefined?'':data.from_quotation_selection_json.complete_profit_rate
                var suitable_profit_rate = data.from_quotation_selection_json.suitable_profit_rate==undefined?'':data.from_quotation_selection_json.suitable_profit_rate
                window.parent.go_to_page_general('./html/inquiry/quotation.html','id='+data.id+'&supplier_id='+supplier_id+'&org_profit_rate='+org_profit_rate+'&matching_profit_rate='+matching_profit_rate+'&complete_profit_rate='+complete_profit_rate+'&suitable_profit_rate='+suitable_profit_rate);
            } else if (obj.event === 'del') { // 删除
                doDelete(data.id);
            }else if (obj.event === 'close') { // 关闭
                doClose(data.id);
            }else if (obj.event === 'complete') { // 完成
                doComplete(data.id);
            }else if (obj.event === 'openInfo') { // 开启
                doOpen(data.id);
            }
        });

        // 关闭
        function doClose(id){
            layer.confirm('确定要关闭吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                send_req('Inquiry/changeState', {id:id,state:3}, function (res) {
                    layer.closeAll('loading');
                    layer.msg('关闭成功', {icon: 1});
                    var where = getWhere();
                    table.reload('infoTable', {where: where});
                });
                return false;
            });
        }

        function doOpen(id){
            layer.confirm('确定要开启吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                send_req('Inquiry/changeState', {id:id,state:1}, function (res) {
                    layer.closeAll('loading');
                    layer.msg('操作成功', {icon: 1});
                    var where = getWhere();
                    table.reload('infoTable', {where: where});
                });
                return false;
            });
        }

        function doComplete(id){
            layer.confirm('确定订单已完成吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                send_req('Inquiry/changeState', {id:id,state:2}, function (res) {
                    layer.closeAll('loading');
                    layer.msg('操作成功', {icon: 1});
                    var where = getWhere();
                    table.reload('infoTable', {where: where});
                });
                return false;
            });
        }
        // 删除
        function doDelete(id) {
            layer.confirm('确定要删除吗？', {
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                send_req('Inquiry/del', {id:id}, function (res) {
                    layer.closeAll('loading');
                    layer.msg('删除成功', {icon: 1});
                    var where = getWhere();
                    table.reload('infoTable', {where: where});
                });
                return false;
            });
        }

        form.on('select(select_user)', function(data) {
            //获取行tr对象
            var elem = data.othis.parents('tr');
            //获取第一列的值，第一列为ID列，
            var id = elem.first().find('td').eq(0).text();
            //选择的select对象值；
            var add_user_id=data.value;
            //处理字段更新的逻辑
            var up_data = {
                id:id,
                add_user_id:add_user_id,
            };
            setOrderUser(up_data);
        });
        function setOrderUser(data) {
            layer.load(2);
            send_req('Inquiry/changeUser', data, function (res) {
                layer.closeAll('loading');
                layer.msg('修改成功', {icon: 1});
                var where = getWhere();
                table.reload('infoTable', {where: where});
            });
            return false;
        }

        function getWhere() {
            var t = $('#searchStationForm [name]').serializeArray();
            var where = {};
            $.each(t, function() {
                where[this.name] = this.value;
            });
            return where;
        }
    });
</script>
</body>
</html>